package com.example.shopping2.a_c_fragment.main

import BaseFragment
import IFragment
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.example.shopping2.R
import com.example.shopping2.a_a_entity.BaseEntity
import com.example.shopping2.a_a_mvp.contract.HomeContract
import com.example.shopping2.a_a_mvp.presenter.presenters.HomePresenter
import com.example.shopping2.a_c_fragment.main.entity.greendao.UserManager

class MainFragment : BaseFragment<HomePresenter>(),IFragment,HomeContract.HomeView {
    private val userImg: ImageView by lazy { findViewById<ImageView>(R.id.user_img) }
    private val userName: TextView by lazy { findViewById<TextView>(R.id.user_name) }
    private val mainOrderWait: ImageView by lazy { findViewById<ImageView>(R.id.main_order_wait) }
    private val mainOrderConfirm: ImageView by lazy { findViewById<ImageView>(R.id.main_order_confirm) }
    private val mainOrderFinish: ImageView by lazy { findViewById<ImageView>(R.id.main_order_finish) }
    private val mainOrderMain: ImageView by lazy { findViewById<ImageView>(R.id.main_order_main) }
    private val mainAddress: LinearLayout by lazy { findViewById<LinearLayout>(R.id.main_address) }
    private val mainSetting: LinearLayout by lazy { findViewById<LinearLayout>(R.id.main_setting) }

    var islogin = false
    private  var userid :Long? = null
    var mybro = MyBro()
    override fun findlayout(): Int {
        return R.layout.fragment_main
    }

    override fun initView() {
        //-----------------------------------------是否登录-------------------------------------
        var sp = activity?.getSharedPreferences("loging", AppCompatActivity.MODE_PRIVATE)

        var boolean = sp?.getBoolean("log", false)
        var name = sp?.getString("name", null)
        var icon = sp?.getString("icon", null)
        userid = sp?.getLong("id", 0)


        if (boolean!!){
            userName.setText(""+name)
            Glide.with(userImg).load(icon).into(userImg)
            islogin = true
        }


        //注册广播
        var intentfilter = IntentFilter()
        intentfilter.addAction("user_login")
        activity?.registerReceiver(mybro,intentfilter)
        //-----------------------------------------点击-------------------------------------
        userImg.setOnClickListener {
            if (islogin){
                //进入头像编辑页面
                // TODO: 2021/7/29 进入个人信息界面

            }else{
                //进入登录页面
                startActivity(Intent(activity,LoginActivity::class.java))
            }
        }
        userName.setOnClickListener {
            if (islogin){
                //进入头像编辑页面


            }else{
                //进入登录页面
                startActivity(Intent(activity,LoginActivity::class.java))


            }
        }
        //-----------------------------------------退出登录-------------------------------------
        mainSetting.setOnClickListener {
            if (islogin){
                //操作退出登录
                var userdao = UserManager()
                userdao.setuserdao(activity!!)
                var select = userdao.select(userid!!)
                if (select == null){

                }
                userdao.delete(userid!!)

                activity?.getSharedPreferences("loging",AppCompatActivity.MODE_PRIVATE)?.edit()
                    ?.putBoolean("log",false)?.commit()


                //-----------------------------------------发送广播-------------------------------------
                var intent = Intent()
                intent.setAction("user_login")
                intent.putExtra("username", "登录/注册")
                intent.putExtra("login_dialog", false)
                intent.putExtra("userimg", "")
                intent.putExtra("type", "finishlogin")
                activity?.sendBroadcast(intent)
                //进入登录页面
                var intent1 = Intent(activity,LoginActivity::class.java)
                intent1.putExtra("type","home")
                startActivity(intent1)
            }else{
                //进入登录页面
                startActivity(Intent(activity,LoginActivity::class.java))

            }

        }

    }

    override fun initData() {


    }

    override fun showHomeData(baseEntity: BaseEntity) {

    }

    override fun onDestroy() {
        super.onDestroy()
        activity?.unregisterReceiver(mybro)
    }
    //-----------------------------------------广播接收者-------------------------------------
    inner class MyBro : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if (intent.action == "user_login"){
                var booleanExtra = intent.getBooleanExtra("login_dialog", false)
                if (booleanExtra){
                    var  user_name = intent.getStringExtra("username").toString()
                    var  user_icon = intent.getStringExtra("userimg").toString()
                    userName.setText(""+user_name)
                    Glide.with(userImg).load(user_icon).into(userImg)
                    islogin = true
                }else{
                    var  user_name = intent.getStringExtra("username").toString()
                    var  user_icon = intent.getStringExtra("userimg").toString()
                    userName.setText(""+user_name)
                    Glide.with(userImg).load(user_icon).into(userImg)
                    islogin = false
                }


            }
        }

    }


}